package com.hoshino.momochi.controller;

import com.hoshino.momochi.annotation.IgnoreLogin;
import com.hoshino.momochi.model.DTO.CollectionDTO;
import com.hoshino.momochi.model.domain.Blog;
import com.hoshino.momochi.model.domain.BlogStar;
import com.hoshino.momochi.model.domain.CollectionBlog;
import com.hoshino.momochi.service.CollectionBlogService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import com.hoshino.momochi.util.PageCheck;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@Slf4j
public class CollectionBlogController {

    @Autowired
    private CollectionBlogService collectionBlogService;

    private final String USER_ID = CodeEnum.USERID.getMessage();

    /**
     * 向指定 收藏夹中 添加 指定博客
     *
     * @param request
     * @param collectionDTO 需求字段 blog_id, collection_id
     * @return
     */
    @RequestMapping("api/collection/addBlogIn")
    public JsonResponse addBlogInCollection(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        BlogStar blogStar = new BlogStar();
        blogStar.setCreateTime(new Date());
        Long uid = (Long) request.getAttribute(USER_ID);
        Long bid = collectionDTO.getBlog_id();
        Long cid = collectionDTO.getCollection_id();
        if (Objects.isNull(bid) || Objects.isNull(cid)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        // 判断 是否 拥有这个收藏夹
        cid = collectionBlogService.getCidByUserId(uid, cid);
        if (Objects.isNull(cid)) {
            // 不属于自己
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        // 查询收藏不为空
        if (Objects.nonNull(collectionBlogService.findUserIsStar(uid, bid))) {
            return JsonResponse.success(CodeEnum.ISEXIST.getCode(), CodeEnum.ISEXIST.getMessage());
        }
        // 一篇博客只能收藏一次 不能 收藏在多个收藏夹中
        blogStar.setCollectionId(cid);
        blogStar.setBlogId(bid);
        collectionBlogService.addBlogInCollection(blogStar);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取指定收藏夹 的 博客列表
     *
     * @param request
     * @param collectionDTO 需求字段 收藏夹id collection_id ,need_page, page_size(null)
     * @return
     */
    @RequestMapping("api/collection/getOneCollection")
    @IgnoreLogin
    public JsonResponse findOneCollection(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long collectionId = collectionDTO.getCollection_id();
        CollectionBlog collectionBlog = new CollectionBlog();
        collectionBlog.setId(collectionId);
        CollectionBlog res;
        // 判断输入是否正确
        if (Objects.isNull(collectionId)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        List<Blog> bl = collectionBlogService.findCollectionBlogs(collectionId);
        Integer pageIndex = collectionDTO.getNeed_page();
        Integer pageSize = collectionDTO.getPage_size();
        return JsonResponse.success(PageCheck.pageCheck(bl, pageIndex, pageSize), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取 当前user_id用户的 收藏夹列表
     *
     * @param request
     * @return
     */
    @RequestMapping("api/collection/getUserCollection")
    @IgnoreLogin
    public JsonResponse findAllUserCollections(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long uid = collectionDTO.getUser_id();
        // 校验输入
        if (Objects.isNull(uid)) {
            return JsonResponse.success(CodeEnum.NOTNULL.getCode(), CodeEnum.NOTNULL.getMessage());
        }
        CollectionBlog collectionBlog = new CollectionBlog();
        collectionBlog.setUserId(uid);
        return JsonResponse.success(collectionBlogService.findAllByUserId(collectionBlog), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 创建 收藏夹
     *
     * @param request
     * @param collectionDTO 需求字段  collection_name 收藏夹名称
     * @return
     */
    @RequestMapping("api/collection/create")
    public JsonResponse createCollection(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        String collectionName = collectionDTO.getCollection_name();
        CollectionBlog collectionBlog = new CollectionBlog();
        collectionBlog.setUserId(uid);
        collectionBlog.setCreateTime(new Date());
        collectionBlog.setCollectionName(collectionName);
        if (StringUtils.isNotBlank(collectionName)) {
            collectionBlogService.createCollectionBlog(collectionBlog);
        }
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SENDFAIL.getMessage());
    }

    /**
     * 删除指定 收藏夹
     *
     * @param request
     * @param collectionDTO collection_id字段
     * @return
     */
    @RequestMapping("api/collection/delCollection")
    public JsonResponse delCollection(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long collectionId = collectionDTO.getCollection_id();
        Long uid = (Long) request.getAttribute(USER_ID);
        CollectionBlog collectionBlog = new CollectionBlog();
        collectionBlog.setUserId(uid);
        collectionBlog.setId(collectionId);
        // 收藏夹id 不为空 且 当前用户 拥有 这个收藏夹 才进行删除操作
        if (Objects.isNull(collectionId) || Optional.ofNullable(uid).map(user_id -> collectionBlogService.getCidByUserId(user_id, collectionId)).isEmpty()) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        collectionBlogService.delCollectionBlog(collectionBlog); // 删除收藏夹
        collectionBlogService.delCollectionAllStar(collectionId); // 删除 收藏夹中 收藏的博客
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 删除 指定收藏夹中 指定博客 (这是在 收藏夹页面操作的）
     *
     * @param request
     * @param collectionDTO blog_id collection_id 字段
     * @return
     */
    @RequestMapping("api/collection/delBlogStar")
    public JsonResponse delBlogStar(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long cid = collectionDTO.getCollection_id();
        Long blogId = collectionDTO.getBlog_id();
        // 校验 输入 以及 校验用户是否拥有该收藏夹
        if (Objects.isNull(blogId) || Objects.isNull(cid) || Objects.isNull(collectionBlogService.getCidByUserId(uid, cid))) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        //收藏夹 id 和 blog_id 取消 该博客的收藏
        BlogStar blogStar = new BlogStar();
        blogStar.setBlogId(blogId);
        blogStar.setCollectionId(cid);
        collectionBlogService.delBlogInCollection(blogStar);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 在 博客 详情页面 取消收藏
     *
     * @param request
     * @param collectionDTO 需求字段 blog_id
     * @return
     */
    @RequestMapping("api/collection/delBlogStarInBlog")
    public JsonResponse delBlogStarInBlog(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long blogId = collectionDTO.getBlog_id();
        Long cid = null;
        // 校验输入是否正确  校验用户是否收藏了这篇博客
        if (Objects.isNull(blogId) || Objects.isNull(cid = collectionBlogService.findUserIsStar(uid, blogId))) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        collectionBlogService.delUserBlogStar(cid, blogId); // 取消收藏该博客
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 传入 blog_id 字段 检查 指定文章 当前用户是否已经收藏过
     *
     * @param request
     * @param collectionDTO
     * @return
     */
    @RequestMapping("api/collection/findBlogIsStar")
    public JsonResponse findBlogIsStar(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long bid = collectionDTO.getBlog_id();
        // 校验输入是否正确
        if (Objects.isNull(bid)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        Long id = collectionBlogService.findUserIsStar(uid, bid);
        return Objects.isNull(id) ? JsonResponse.success(CodeEnum.NOTEXIST.getCode(), CodeEnum.NOTEXIST.getMessage()) : JsonResponse.success(CodeEnum.ISEXIST.getCode(), CodeEnum.ISEXIST.getMessage());
    }
}
